<template>
  <basic-container v-loading.fullscreen="deliveryLoading" element-loading-text="数据同步中请等待...">
    <avue-crud
      ref="crud"
      v-model="form"
      :data="data"
      :option="option"
      :loading="loading"
      :page.sync="page"
      @selection-change="selectionChange"
      @search-change="searchChange"
      @search-reset="searchReset"
      @size-change="sizeChange"
      @current-change="currentChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="primary" size="small" icon="el-icon-plus" v-if="permission.deliveryOrdersManage_add" @click="add">新增</el-button>
        <el-button type="warning" size="small" icon="el-icon-truck" v-if="permission.deliveryOrdersManage_delivery" @click="openDeliveryInfoDialog">送货</el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button type="text" icon="el-icon-view" size="small" @click.stop="handleViewDetails(scope.row)">详情</el-button>
        <el-button icon="el-icon-edit" size="small" type="text" v-if="permission.deliveryOrdersManage_edit" @click="rowUpdate(scope.row)">编辑</el-button>
        <el-button icon="el-icon-delete" size="small" type="text" v-if="permission.deliveryOrdersManage_delete" @click="rowDel(scope.row)">删除</el-button>
        <el-button icon="el-icon-printer" size="small" type="text" v-if="permission.deliveryOrdersManage_printBarCode" @click="printBarCode(scope.row)">打印条码</el-button>
        <el-button
          icon="el-icon-printer"
          size="small"
          type="text"
          v-if="permission.deliveryOrdersManage_printDeliveryOrder"
          :disabled="scope.row.isPrintedAllSeq == 0"
          @click="printDeliveryOrder(scope.row)"
          >打印送货单</el-button
        >
      </template>
      <!--      <template slot="menuRight">-->
      <!--        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>-->
      <!--      </template>-->
    </avue-crud>
    <el-dialog
      v-dragDirectives
      ref="dialog"
      :title="dialogTitle"
      append-to-body
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      top="5vh"
      width="70%"
      @close="closeDialog"
    >
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>送货明细</h3>
      <el-row style="margin-bottom: 10px">
        <el-button v-if="dialogTitle != '详情'" type="primary" size="small" icon="el-icon-plus" @click="openDeliveryOrderDetailSection('part')">添加送货明细</el-button>
        <el-button v-if="dialogTitle != '详情'" type="danger" size="small" icon="el-icon-delete" @click="deliveryOrderDelete">删 除</el-button>
      </el-row>
      <el-table ref="deliveryDetailTable" v-loading="loading" height="500px" :data="deliveryDetailList" border style="width: 100%" @selection-change="basicInfoSelectionChange">
        <el-table-column type="selection" width="50" align="center"></el-table-column>
        <el-table-column type="index" width="50" align="center"></el-table-column>
        <template v-for="item in deliveryDetailColumns">
          <el-table-column
            v-if="item.prop != 'thisTimeDeliveryQuantity' && item.prop != 'productDate' && item.prop != 'productLotNo'"
            :label="item.label"
            :prop="item.prop"
            :key="item.prop"
            width="150"
          >
          </el-table-column>
        </template>
        <el-table-column fixed="right" label="本次发货数量" prop="currentDeliveryQuantity" width="100" v-if="dialogTitle != '详情'">
          <template slot-scope="scope">
            <el-input-number
              v-model="scope.row.currentDeliveryQuantity"
              :controls="false"
              :precision="0"
              style="width: 100%"
              :disabled="dialogTitle == '详情'"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="生产日期" prop="vendorDate" width="180" v-if="dialogTitle != '详情'">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.vendorDate"
              type="date"
              name="vendorDate"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期"
              style="width: 100%"
              :disabled="dialogTitle == '详情'"
            ></el-date-picker>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="生产批次" prop="vendorLotNo" width="100" v-if="dialogTitle != '详情'">
          <template slot-scope="scope">
            <el-input v-model="scope.row.vendorLotNo" style="width: 100%" name="vendorLotNo" :disabled="dialogTitle == '详情'"></el-input>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="80" v-if="dialogTitle != '详情'">
          <template slot-scope="scope">
            <el-button size="small" type="text" icon="el-icon-delete" @click="deliveryDetailRowDel(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="showDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
        <el-button v-if="dialogTitle === '新增'" type="primary" size="small" @click="submit"><i class="el-icon-circle-plus-outline"></i><span>保 存</span> </el-button>
        <el-button v-if="dialogTitle === '编辑'" type="primary" size="small" @click="submit"><i class="el-icon-circle-check"></i><span>修 改</span> </el-button>
      </span>
    </el-dialog>
    <el-dialog
      v-dragDirectives
      ref="dialog"
      :title="selectDeliveryDetailTitle"
      append-to-body
      :visible.sync="showSelectDeliveryDetailDialog"
      top="5vh"
      width="70%"
      @close="closeSelectDeliveryDetailDialog"
    >
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>送货明细</h3>
      <h4>客户名称：{{ deliveryStatistic.customerName }}</h4>
      <p>订单个数：{{ deliveryStatistic.orderNumber }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;物料种类：{{ deliveryStatistic.partSpeciesNumber }}</p>
      <p>
        行数：{{ deliveryStatistic.rowNumber }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;已完成打印的行数：{{
          deliveryStatistic.printedNumber
        }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;未完成打印的行数：{{ deliveryStatistic.unprintedNumber }}
      </p>
      <avue-crud
        v-if="showSelectDeliveryDetailDialog"
        ref="crud"
        :data="selectDeliveryDetailData"
        :option="selectDeliveryDetailOption"
        :loading="selectDeliveryDetailLoading"
        :page.sync="selectDeliveryDetailPage"
        @size-change="hselectDeliveryDetailSizeChange"
        @current-change="selectDeliveryDetailCurrentChange"
        @selection-change="deliveryDetailSelectionChange"
      >
        <template slot-scope="scope" slot="menu">
          <el-button v-if="selectDeliveryDetailTitle === '详情'" type="text" icon="el-icon-view" size="small" @click.stop="viewBarCodeDetail(scope.row)">条码明细</el-button>
        </template>
      </avue-crud>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="showSelectDeliveryDetailDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
        <el-button v-if="selectDeliveryDetailTitle != '详情'" type="primary" size="small" @click="next"
          ><i class="el-icon-circle-plus-outline"></i><span>下一步</span></el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      v-dragDirectives
      ref="dialog"
      title="打印送货单"
      append-to-body
      :visible.sync="printDeliveryOrderDialog"
      :fullscreen="true"
      @close="closePrintDeliveryOrderDialog"
    >
      <div id="printContent">
        <h2 style="text-align: center">供应商送货单</h2>
        <div style="display: flex; justify-content: space-between; align-items: flex-end">
          <div style="margin-bottom: 15px">送货单：{{ printDeliveryOrderInfo.deliveryOrderNo }}</div>
          <div><svg id="barcode"></svg></div>
        </div>
        <el-row>
          <el-col :span="12">供应商名称：{{ printDeliveryOrderInfo.vendorName }}</el-col>
          <el-col :span="12">客户名称：{{ printDeliveryOrderInfo.customerName }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="12">供应商代码：{{ printDeliveryOrderInfo.vendorCode }}</el-col>
          <el-col :span="12">客户地址：{{ printDeliveryOrderInfo.customerAddress }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="12">供应商联系人：{{ printDeliveryOrderInfo.vendorPrincipal }}</el-col>
          <el-col :span="12">客户联系人：{{ printDeliveryOrderInfo.customerPrincipal }}</el-col>
        </el-row>
        <el-row>
          <el-col :span="12">供应商联系电话：{{ printDeliveryOrderInfo.vendorPhone }}</el-col>
          <el-col :span="12">客户联系电话：{{ printDeliveryOrderInfo.customerPhone }}</el-col>
        </el-row>
        <table width="100%;" cellpadding="0" cellspacing="0" border="1" bordercolor="#dfe6ec" style="text-align: center">
          <thead>
            <tr>
              <th>序号</th>
              <th>客户订单</th>
              <th>行号</th>
              <th>物料编码</th>
              <th>物料名称</th>
              <th>物料型号</th>
              <th>数量</th>
              <th>生产日期</th>
              <th>生产批次</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(item, index) in printDeliveryDetailData" :key="index">
              <td>{{ index + 1 }}</td>
              <td>{{ item.sourceNo }}</td>
              <td>{{ item.sourceSheetLot }}</td>
              <td>{{ item.vendorPartNo }}</td>
              <td>{{ item.vendorPartName }}</td>
              <td>{{ item.vendorPartModel }}</td>
              <td>{{ item.sourceOrderQuantity }}</td>
              <td>{{ item.vendorDate }}</td>
              <td>{{ item.vendorLotNo }}</td>
            </tr>
          </tbody>
        </table>
        <p>备注：</p>
        <p>1.货物外箱包装是否完好：是&nbsp;&nbsp;&nbsp;&nbsp;否</p>
        <p style="margin-bottom: 100px">2.其他异常，请详细说明：</p>
        <el-row>
          <el-col :span="12"><p>送货人：</p></el-col>
          <el-col :span="12"><p>收货人：</p></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"><p>送货日期：</p></el-col>
          <el-col :span="12"><p>收货日期：</p></el-col>
        </el-row>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="printDeliveryOrderDialog = false"><i class="el-icon-circle-close"></i><span>取消</span></el-button>
        <el-button v-print="printViewInfo" type="primary" size="small"><i class="el-icon-circle-plus-outline"></i><span>打印</span></el-button>
      </span>
    </el-dialog>
    <el-dialog
      v-dragDirectives
      ref="generateBarcodeDialog"
      :title="generateBarcodeTitle"
      append-to-body
      :visible.sync="generateBarcodeDialog"
      :fullscreen="true"
      @close="closeGenerateBarcodeDialog"
    >
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>送货明细</h3>
      <div style="display: flex">
        <h4>客户名称：{{ deliveryStatistic.customerName }}</h4>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <h4>送货单：{{ deliveryStatistic.partSpeciesNumber }}</h4>
      </div>
      <avue-crud :data="generateBarcodeOrderDetailData" :option="generateBarcodeOrderDetailOption"></avue-crud>
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>包装配置</h3>
      <el-form ref="packagingConfigForm" :model="packagingConfig" :rules="rules">
        <el-row>
          <el-col :span="6">
            <el-form-item label="包装层级:" label-width="90px" prop="packMaxLevel">
              <el-select
                v-model="packagingConfig.packMaxLevel"
                placeholder="请选择包装层级"
                :disabled="isGenerateBarCode || generateBarcodeTitle === '详情'"
                @change="changePackMaxLevel"
              >
                <el-option label="1" value="1"></el-option>
                <el-option label="2" value="2"></el-option>
                <el-option label="3" value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="序列号规则:" label-width="100px" prop="sequenceDefinitionName">
              <el-input v-model="packagingConfig.sequenceDefinitionName" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="packagingConfig.packMaxLevel == 1 || packagingConfig.packMaxLevel == 2 || packagingConfig.packMaxLevel == 3">
          <el-col :span="12">
            <el-form-item label="包装层级1(小包) ：1个小包，装" label-width="218px" prop="packQuantitySpecFirst"
              ><el-input-number
                style="width: 70%"
                :controls="false"
                :precision="0"
                :disabled="isGenerateBarCode || generateBarcodeTitle === '详情'"
                v-model="packagingConfig.packQuantitySpecFirst"
                placeholder="请输入数量"
              ></el-input-number
              >个产品
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="packagingConfig.packMaxLevel == 2 || packagingConfig.packMaxLevel == 3">
          <el-col :span="12">
            <el-form-item label="包装层级2(大包) ：1个大包，装" label-width="218px" prop="subPackQuantitySecond"
              ><el-input-number
                style="width: 70%"
                :controls="false"
                :precision="0"
                :disabled="isGenerateBarCode || generateBarcodeTitle === '详情'"
                v-model="packagingConfig.subPackQuantitySecond"
                placeholder="请输入数量"
              ></el-input-number
              >个小包
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="packagingConfig.packMaxLevel == 3">
          <el-col :span="12">
            <el-form-item label="包装层级3(外箱) ：1个外箱，装" label-width="218px" prop="subPackQuantityThird"
              ><el-input-number
                style="width: 70%"
                :controls="false"
                :precision="0"
                :disabled="isGenerateBarCode || generateBarcodeTitle === '详情'"
                v-model="packagingConfig.subPackQuantityThird"
                placeholder="请输入数量"
              ></el-input-number
              >个大包
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>条码明细</h3>
      <div v-if="generateBarcodeTitle != '详情'" style="display: flex; justify-content: space-between; margin-bottom: 10px">
        <div>
          <el-button type="primary" size="small" :disabled="isGenerateBarCode" @click="previewBarCode"><span>预览条码</span></el-button>
        </div>
        <div>
          <el-button type="primary" size="small" @click="generateBarcode"><span>生成条码</span></el-button>
          <el-button type="primary" size="small" @click="printBarCodeDetail"><span>打印条码</span></el-button>
          <el-button type="primary" size="small" @click="scrapBarCode"><span>报废条码</span></el-button>
        </div>
      </div>
      <table class="barCodeDetailTable" width="100%;" cellpadding="0" cellspacing="0" border="1" bordercolor="#dfe6ec" style="text-align: center">
        <thead>
          <tr>
            <th>序号</th>
            <th v-if="generateBarcodeTitle != '详情'"><input type="checkbox" id="firstTheadChecked" /></th>
            <th>小包条码({{ barCodePreviewInfo.packQuantityFirst }})</th>
            <th>小包包装数量(包规数量：{{ barCodePreviewInfo.packQuantitySpecFirst }})</th>
            <th v-if="generateBarcodeTitle != '详情'"><input type="checkbox" id="secondTheadChecked" /></th>
            <th>大包条码({{ barCodePreviewInfo.packQuantitySecond }})</th>
            <th>大包包装数量(包规数量：{{ barCodePreviewInfo.packQuantitySpecSecond }})</th>
            <th v-if="generateBarcodeTitle != '详情'"><input type="checkbox" id="thirdTheadChecked" /></th>
            <th>外箱条码({{ barCodePreviewInfo.packQuantityThird }})</th>
            <th>外箱包装数量(包规数量：{{ barCodePreviewInfo.packQuantitySpecThird }})</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in barCodePreviewInfo.scmSequenceDetailDTOList" :key="index">
            <td>{{ index + 1 }}</td>
            <td v-if="generateBarcodeTitle != '详情'">
              <input v-if="item.sequenceNumberFirst && item.sequenceNumberFirst != null" type="checkbox" :name="item.sequenceNumberFirst" />
            </td>
            <td>{{ item.sequenceNumberFirst }}</td>
            <td>{{ item.quantityFirst }}<span v-if="item.isFirstPackTail">(尾箱)</span></td>
            <td v-if="generateBarcodeTitle != '详情'">
              <input v-if="item.sequenceNumberSecond && item.sequenceNumberSecond != null" type="checkbox" :name="item.sequenceNumberSecond" />
            </td>
            <td>{{ item.sequenceNumberSecond }}</td>
            <td>{{ item.quantitySecond }}<span v-if="item.isSecondPackTail">(尾箱)</span></td>
            <td v-if="generateBarcodeTitle != '详情'">
              <input v-if="item.sequenceNumberThird && item.sequenceNumberThird != null" type="checkbox" :name="item.sequenceNumberThird" />
            </td>
            <td>{{ item.sequenceNumberThird }}</td>
            <td>{{ item.quantityThird }}<span v-if="item.isThirdPackTail">(尾箱)</span></td>
          </tr>
        </tbody>
      </table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="generateBarcodeDialog = false"><i class="el-icon-circle-close"></i><span>取消</span></el-button>
      </span>
    </el-dialog>
    <el-dialog v-dragDirectives ref="deliveryDialog" title="送货" append-to-body :visible.sync="deliveryDialog" top="5vh" width="70%" @close="closeDeliveryDialog">
      <div class="sail-header-dialog-content">
        <div v-for="(item, index) in deliveryList" :key="index">
          <h4>送货单：{{ item.scmDeliveryOrderNo }}</h4>
          <el-table ref="deliveryDetailTable" v-loading="loading" height="300px" :data="item.scmDeliverySheetDetailList" border style="width: 100%">
            <el-table-column type="index" width="50" align="center"></el-table-column>
            <template v-for="columnItem in generateBarcodeOrderDetailOption.column">
              <el-table-column :label="columnItem.label" :prop="columnItem.prop" :key="columnItem.prop"> </el-table-column>
            </template>
          </el-table>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="deliveryDialog = false"><i class="el-icon-circle-close"></i><span>取消</span></el-button>
        <el-button type="primary" size="small" @click="submitDelivery"><i class="el-icon-circle-plus-outline"></i><span>送货</span></el-button>
      </span>
    </el-dialog>
    <el-dialog append-to-body title="打印条码" :visible.sync="printInfoDialog" width="35%" @close="closePrintInfoDialog">
      <avue-form ref="printForm" v-model="printInfo" :option="printInfoOption"> </avue-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="printSubmit"><i class="el-icon-printer"></i><span>打 印</span></el-button>
        <el-button size="small" @click="printInfoDialog = false"><i class="el-icon-circle-close"></i><span>关 闭</span></el-button>
      </span>
    </el-dialog>
    <common-modal-template ref="commonModalTemplate" :key="timer" :option="tableOption" @getSelectedRows="getSelectedRows" @newKey="newKey"></common-modal-template>
  </basic-container>
</template>

<script>
import {
  getList,
  submitDeliveryOrder,
  getDetail,
  queryDeliveryOrderDetail,
  removeDeliveryOrder,
  deliveryPreviewStatistic,
  getVendorInfo,
  getVendorDetail,
  getDeliveryPreviewInfo,
  getPreviewBarCode,
  getCreateBarCode,
  sequencePirnt,
  sequenceScrap,
  getSequenceDetail,
  delivering,
  deliveryOrderPrint
} from '@/api/scm/delivery-orders-manage';
import CommonModalTemplate from '@/components/common-modal-template/common-modal-template';
import JsBarcode from 'jsbarcode';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
export default {
  name: 'ScmCustomerOrdersManage',
  components: { CommonModalTemplate, userTableConfig },
  mixins: [userConfig()],
  data() {
    return {
      query: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      loading: false,
      data: [],
      option: {
        height: 'calc(100vh - 488px)',
        tip: false,
        border: true,
        index: true,
        indexLabel: '序号',
        selection: true,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        columnBtn: false,
        searchMenuSpan: 4,
        searchMenuPosition: 'right',
        searchLabelWidth: 125,
        menuWidth: 350,
        column: [
          { label: '送货单', prop: 'deliveryOrderNo', search: true },
          { label: '客户名称', prop: 'customerName' },
          {
            label: '条码打印状态',
            prop: 'isPrintedAllSeq',
            search: true,
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=scm_printed_sequence_status',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '送货单打印状态',
            prop: 'isPrintedSheet',
            search: true,
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=scm_printed_sheet_status',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '送货状态',
            prop: 'deliveryStatus',
            search: true,
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=scm_deliver_status',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '创建时间',
            prop: 'createTime',
            search: true,
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间'
          }
        ]
      },
      selectionList: [],
      rowId: '',
      rowNo: '',
      modalName: '',
      tableOption: {},
      timer: new Date().getTime(),
      showDialog: false,
      deliveryDetailColumns: [
        { label: '客户名称', prop: 'vendorName' },
        { label: '客户订单', prop: 'sourceNo' },
        { label: '行号', prop: 'sourceSheetLot' },
        { label: '客户物料编码', prop: 'vendorPartNo' },
        { label: '客户物料名称', prop: 'vendorPartName' },
        { label: '订单数量', prop: 'sourceOrderQuantity' },
        { label: '已发货数量', prop: 'deliveredQuantity' },
        { label: '未发货数量', prop: 'unDeliveredQuantity' }
      ],
      deliveryDetailList: [],
      deliveryDetailSelectList: [],
      showSelectDeliveryDetailDialog: false,
      selectDeliveryDetailTitle: '',
      selectDeliveryDetailData: [],
      selectDeliveryDetailOption: {
        height: '400',
        tip: false,
        border: true,
        index: true,
        indexLabel: '序号',
        selection: true,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        menu: false,
        menuWidth: 100,
        searchMenuSpan: 4,
        searchMenuPosition: 'right',
        searchLabelWidth: 125,
        column: [
          { label: '客户名称', prop: 'vendorName' },
          { label: '客户订单', prop: 'sourceNo' },
          { label: '行号', prop: 'sourceSheetLot' },
          { label: '客户物料编码', prop: 'vendorPartNo' },
          { label: '客户物料名称', prop: 'vendorPartName' },
          { label: '客户物料型号', prop: 'vendorPartModel' },
          { label: '订单数量', prop: 'sourceOrderQuantity' },
          { label: '已发货数量', prop: 'deliveredQuantity' },
          { label: '未发货数量', prop: 'unDeliveredQuantity' },
          { label: '本次发货数量', prop: 'currentDeliveryQuantity' },
          { label: '生产日期', prop: 'vendorDate' },
          { label: '生产批次', prop: 'vendorLotNo' },
          {
            label: '打印状态',
            prop: 'isPrintedSeq',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=scm_printed_sequence_status',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          }
        ]
      },
      selectDeliveryDetailLoading: false,
      selectDeliveryDetailPage: {
        total: 0,
        currentPage: 1,
        pageSize: 10
      },
      deliveryDetailSelectionList: [],
      deliveryStatistic: {},
      printDeliveryOrderDialog: false,
      printDeliveryOrderInfo: {},
      printDeliveryDetailData: [],
      printDeliveryDetailOption: {
        height: '300',
        tip: false,
        border: true,
        index: true,
        indexLabel: '序号',
        selection: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        menu: false,
        column: [
          { label: '客户订单', prop: 'sourceNo' },
          { label: '行号', prop: 'sourceSheetLot' },
          { label: '物料编码', prop: 'vendorPartNo' },
          { label: '物料名称', prop: 'vendorPartName' },
          { label: '物料型号', prop: 'partModel' },
          { label: '数量', prop: 'sourceOrderQuantity' },
          { label: '生产日期', prop: 'vendorDate' },
          { label: '生产批次', prop: 'vendorLotNo' }
        ]
      },
      generateBarcodeDialog: false,
      generateBarcodeTitle: '生成条码',
      generateBarcodeOrderDetailData: [],
      generateBarcodeOrderDetailOption: {
        height: '100',
        tip: false,
        border: true,
        index: true,
        indexLabel: '序号',
        selection: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        menu: false,
        column: [
          { label: '客户订单', prop: 'sourceNo' },
          { label: '行号', prop: 'sourceSheetLot' },
          { label: '客户物料编码', prop: 'vendorPartNo' },
          { label: '客户物料名称', prop: 'vendorPartName' },
          { label: '客户物料型号', prop: 'vendorPartModel' },
          { label: '订单数量', prop: 'sourceOrderQuantity' },
          { label: '已发货数量', prop: 'deliveredQuantity' },
          { label: '未发货数量', prop: 'unDeliveredQuantity' },
          { label: '本次发货数量', prop: 'currentDeliveryQuantity' },
          { label: '生产日期', prop: 'vendorDate' },
          { label: '生产批次', prop: 'vendorLotNo' }
        ]
      },
      packagingConfig: {},
      rules: {
        packMaxLevel: [{ required: true, message: '请选择包装层级', trigger: 'blur' }],
        sequenceDefinitionName: [{ required: true, message: '序列号规则不能为空', trigger: 'blur' }],
        packQuantitySpecFirst: [{ required: true, message: '请输入包装层级1', trigger: 'blur' }],
        subPackQuantitySecond: [{ required: true, message: '请输入包装层级2', trigger: 'blur' }],
        subPackQuantityThird: [{ required: true, message: '请输入包装层级3', trigger: 'blur' }]
      },
      barCodePreviewInfo: {},
      deliveryDialog: false,
      deliveryList: [],
      deliveryLoading: false,
      printInfoDialog: false,
      printInfo: {},
      printInfoOption: {
        labelWidth: 150,
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: '标签模版',
            prop: 'labelTemplateName',
            span: 20,
            type: 'search',
            placeholder: '请选择 标签模板',
            disabled: true,
            rules: [
              {
                required: true,
                message: '请选择标签模板',
                trigger: 'blur'
              }
            ],
            appendClick: () => {
              this.openLabelTemplateModal();
            }
          },
          {
            label: '打印机',
            prop: 'equipmentName',
            span: 20,
            type: 'search',
            placeholder: '请选择 打印机',
            disabled: true,
            rules: [
              {
                required: true,
                message: '请选择打印机',
                trigger: 'blur'
              }
            ],
            appendClick: () => {
              this.openPrintersModal();
            }
          }
        ]
      },
      isGenerateBarCode: false,
      printViewInfo: {
        id: 'printContent', //打印区域的唯一的id属性
        openCallback(that) {
          that.handleAfterPrint();
        } // 调用打印时的callback
      }
    };
  },
  computed: {
    ...mapGetters(['permission'])
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    onLoad(page, params = {}) {
      if (this.query.createTime) {
        this.query.startTime = this.$moment(this.query.createTime[0]).format('YYYY-MM-DD HH:mm:ss');
        this.query.endTime = this.$moment(this.query.createTime[1]).format('YYYY-MM-DD HH:mm:ss');
        delete this.query.createTime;
      }
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this._forEach(this.data, (item) => {
          item.isPrintedAllSeq = String(item.isPrintedAllSeq);
          item.isPrintedSheet = String(item.isPrintedSheet);
          item.deliveryStatus = String(item.deliveryStatus);
        });
        this.loading = false;
      });
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    handleViewDetails(row) {
      this.showSelectDeliveryDetailDialog = true;
      this.selectDeliveryDetailTitle = '详情';
      this.selectDeliveryDetailOption.selection = false;
      this.selectDeliveryDetailOption.menu = true;
      this.$set(this.deliveryStatistic, 'customerName', row.customerName);
      deliveryPreviewStatistic(row.deliveryOrderId).then((res) => {
        this.$set(this.deliveryStatistic, 'orderNumber', this._get(res, 'data.data.orderNumber', ''));
        this.$set(this.deliveryStatistic, 'partSpeciesNumber', this._get(res, 'data.data.partSpeciesNumber', ''));
        this.$set(this.deliveryStatistic, 'printedNumber', this._get(res, 'data.data.printedNumber', ''));
        this.$set(this.deliveryStatistic, 'rowNumber', this._get(res, 'data.data.rowNumber', ''));
        this.$set(this.deliveryStatistic, 'unprintedNumber', this._get(res, 'data.data.unprintedNumber', ''));
      });
      this.getDeliveryOrderDetail(row);
    },
    closeSelectDeliveryDetailDialog() {
      this.deliveryStatistic = {};
      this.selectDeliveryDetailData = [];
      this.selectDeliveryDetailPage = {
        pageSize: 20,
        currentPage: 1,
        total: 0
      };
    },
    viewBarCodeDetail(row) {
      this.generateBarcodeDialog = true;
      this.generateBarcodeTitle = '详情';
      this.generateBarcodeOrderDetailData = [row];
      getVendorDetail(this.generateBarcodeOrderDetailData[0].vendorId).then((res) => {
        this.$set(this.packagingConfig, 'sequenceDefinitionId', this._get(res, 'data.data.sequenceDefinitionId', ''));
        this.$set(this.packagingConfig, 'sequenceDefinitionName', this._get(res, 'data.data.sequenceDefinitionName', ''));
      });
      this.querySequenceDetail();
    },
    closeGenerateBarcodeDialog() {
      this.generateBarcodeOrderDetailData = [];
      this.packagingConfig = {};
      this.$refs.packagingConfigForm.resetFields();
      this.barCodePreviewInfo = {};
    },
    add() {
      this.showDialog = true;
      this.dialogTitle = '新增';
    },
    openDeliveryOrderDetailSection() {
      this.modalName = 'deliveryOrderDetailModal';
      this.$refs.commonModalTemplate.showModal();
      this.tableOption = {
        modalTitle: '选择送货明细',
        url: '/api/mes-warehouse/scm/vendor/page-query/source-order-detail',
        isPage: true,
        rowSelectionType: 'multiple',
        columnDefs: [
          { label: '客户名称', prop: 'vendorName', search: true },
          { label: '客户订单', prop: 'sourceNo', search: true },
          { label: '行号', prop: 'sourceSheetLot' },
          { label: '客户物料编码', prop: 'vendorPartNo' },
          { label: '客户物料名称', prop: 'vendorPartName' },
          { label: '订单数量', prop: 'sourceOrderQuantity' },
          { label: '已发货数量', prop: 'deliveredQuantity' },
          { label: '未发货数量', prop: 'unDeliveredQuantity' }
        ]
      };
    },
    /**
     * 双击模态框行
     */
    getSelectedRows(row) {
      switch (this.modalName) {
        case 'deliveryOrderDetailModal':
          if (this.dialogTitle === '新增') {
            this.deliveryDetailList = this._map(row, (item) => {
              return {
                vendorId: item.vendorId,
                vendorCode: item.vendorCode,
                vendorName: item.vendorName,
                sourceId: item.sourceId,
                sourceNo: item.sourceNo,
                sourceType: item.sourceType,
                sourceDetailId: item.sourceDetailId,
                sourceSheetLot: item.sourceSheetLot,
                vendorPartMasterId: item.vendorPartMasterId,
                vendorPartNo: item.vendorPartNo,
                vendorPartName: item.vendorPartName,
                sourceOrderQuantity: item.sourceOrderQuantity,
                deliveredQuantity: item.deliveredQuantity,
                unDeliveredQuantity: item.unDeliveredQuantity,
                currentDeliveryQuantity: item.unDeliveredQuantity,
                vendorDate: '',
                vendorLotNo: ''
              };
            });
          } else {
            this._forEach(row, (item) => {
              this.deliveryDetailList.push({
                vendorId: item.vendorId,
                vendorCode: item.vendorCode,
                vendorName: item.vendorName,
                sourceId: item.sourceId,
                sourceNo: item.sourceNo,
                sourceType: item.sourceType,
                sourceDetailId: item.sourceDetailId,
                sourceSheetLot: item.sourceSheetLot,
                vendorPartMasterId: item.vendorPartMasterId,
                vendorPartNo: item.vendorPartNo,
                vendorPartName: item.vendorPartName,
                sourceOrderQuantity: item.sourceOrderQuantity,
                deliveredQuantity: item.deliveredQuantity,
                unDeliveredQuantity: item.unDeliveredQuantity,
                currentDeliveryQuantity: item.unDeliveredQuantity,
                vendorDate: '',
                vendorLotNo: ''
              });
            });
          }
          break;
        case 'labelTemplateModal':
          this.$set(this.printInfo, 'labelTemplateId', row.id);
          this.$set(this.printInfo, 'labelTemplateName', row.fileName);
          break;
        case 'printersModal':
          this.$set(this.printInfo, 'equipmentId', row.id);
          this.$set(this.printInfo, 'equipmentName', row.name);
          break;
        default:
          break;
      }
    },
    deliveryDetailRowDel(index) {
      this.deliveryDetailList.splice(index, 1);
    },
    basicInfoSelectionChange(list) {
      this.deliveryDetailSelectList = list;
    },
    deliveryOrderDelete() {
      if (this.deliveryDetailSelectList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this._forEach(this.deliveryDetailSelectList, (item) => {
          let index = this.deliveryDetailList.indexOf(item);
          this.deliveryDetailList.splice(index, 1);
        });
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
        this.$refs.deliveryDetailTable.clearSelection();
      });
    },
    printBarCode(row) {
      this.rowId = row.deliveryOrderId;
      this.showSelectDeliveryDetailDialog = true;
      this.selectDeliveryDetailTitle = '选择送货明细';
      this.selectDeliveryDetailOption.selection = true;
      this.selectDeliveryDetailOption.menu = false;
      this.$set(this.deliveryStatistic, 'customerName', row.customerName);
      deliveryPreviewStatistic(row.deliveryOrderId).then((res) => {
        this.$set(this.deliveryStatistic, 'orderNumber', this._get(res, 'data.data.orderNumber', ''));
        this.$set(this.deliveryStatistic, 'partSpeciesNumber', this._get(res, 'data.data.partSpeciesNumber', ''));
        this.$set(this.deliveryStatistic, 'printedNumber', this._get(res, 'data.data.printedNumber', ''));
        this.$set(this.deliveryStatistic, 'rowNumber', this._get(res, 'data.data.rowNumber', ''));
        this.$set(this.deliveryStatistic, 'unprintedNumber', this._get(res, 'data.data.unprintedNumber', ''));
      });
      this.getDeliveryOrderDetail(row);
    },
    getDeliveryOrderDetail(row) {
      this.selectDeliveryDetailLoading = true;
      queryDeliveryOrderDetail(
        this.selectDeliveryDetailPage.currentPage,
        this.selectDeliveryDetailPage.pageSize,
        Object.assign({ scmDeliveryOrderId: row.deliveryOrderId })
      ).then((res) => {
        const data = res.data.data;
        this.selectDeliveryDetailPage.total = data.total;
        this.selectDeliveryDetailData = data.records;
        this._forEach(this.selectDeliveryDetailData, (item) => {
          item.isPrintedSeq = String(item.isPrintedSeq);
        });
        this.selectDeliveryDetailLoading = false;
      });
    },
    hselectDeliveryDetailSizeChange(pageSize) {
      this.selectDeliveryDetailPage.pageSize = pageSize;
      this.getDeliveryOrderDetail();
    },
    selectDeliveryDetailCurrentChange(currentPage) {
      this.selectDeliveryDetailPage.currentPage = currentPage;
      this.getDeliveryOrderDetail();
    },
    deliveryDetailSelectionChange(list) {
      this.deliveryDetailSelectionList = list;
    },
    rowUpdate(row) {
      if (row.deliveryStatus == 3) {
        this.$message.warning('送货单已送货，不可修改！');
        return;
      }
      this.showDialog = true;
      this.dialogTitle = '编辑';
      this.rowId = row.deliveryOrderId;
      this.rowNo = row.deliveryOrderNo;
      getDetail(row.deliveryOrderId).then((res) => {
        this.deliveryDetailList = this._get(res, 'data.data', []);
      });
    },
    rowDel(row) {
      if (row.deliveryStatus == 3) {
        this.$message.error('送货单已送货，不可删除！');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeDeliveryOrder(row.deliveryOrderId).then((res) => {
          this.$message.success(res.data.msg);
          this.onLoad(this.page);
        });
      });
    },
    submit() {
      if (this.deliveryDetailList.length === 0) {
        this.$message.warning('请选择送货明细!');
        return;
      }
      let vendorName = this.deliveryDetailList[0].vendorName;
      for (let i = 0; i < this.deliveryDetailList.length; i++) {
        if (vendorName != this.deliveryDetailList[i].vendorName) {
          this.$message.warning('同个送货单不可选择多个客户，请重新选择！');
          return;
        }
        if (Number(this.deliveryDetailList[i].unDeliveredQuantity) < Number(this.deliveryDetailList[i].currentDeliveryQuantity)) {
          this.$message.warning('本次发货数量不可超过未发货数量!');
          return;
        }
        if (!this.deliveryDetailList[i].currentDeliveryQuantity) {
          this.$message.warning('本次发货数量不能为空!');
          return;
        }
        if (Number(this.deliveryDetailList[i].currentDeliveryQuantity < 1)) {
          this.$message.warning('本次发货数量需为正整数!');
          return;
        }
        if (!this.deliveryDetailList[i].vendorDate) {
          this.$message.warning('生产日期不能为空！');
          return;
        }
        if (!this.deliveryDetailList[i].vendorLotNo) {
          this.$message.warning('生产批次不能为空！');
          return;
        }
      }
      let params = {
        vendorId: this.deliveryDetailList[0].vendorId,
        vendorCode: this.deliveryDetailList[0].vendorCode,
        vendorName: this.deliveryDetailList[0].vendorName,
        sheetItemList: this.deliveryDetailList
      };
      if (this.dialogTitle === '编辑') {
        params['id'] = this.rowId;
        params['sheetNo'] = this.rowNo;
      }
      submitDeliveryOrder(params).then((res) => {
        this.$message.success('提交成功!');
        this.showDialog = false;
        this.onLoad(this.page);
      });
    },
    closeDialog() {
      this.deliveryDetailList = [];
    },
    printDeliveryOrder(row) {
      if (row.deliveryStatus != 3) {
        this.$message.warning('送货单还未送货，请先操作送货！');
        return;
      }
      this.rowId = row.deliveryOrderId;
      this.printDeliveryOrderDialog = true;
      this.$set(this.printDeliveryOrderInfo, 'deliveryOrderNo', row.deliveryOrderNo);
      getVendorInfo(row.deliveryOrderId).then((res) => {
        this.$set(this.printDeliveryOrderInfo, 'vendorName', this._get(res, 'data.data.vendorName', ''));
        this.$set(this.printDeliveryOrderInfo, 'customerName', this._get(res, 'data.data.customerName', ''));
        this.$set(this.printDeliveryOrderInfo, 'vendorCode', this._get(res, 'data.data.vendorCode', ''));
        this.$set(this.printDeliveryOrderInfo, 'customerAddress', this._get(res, 'data.data.customerAddress', ''));
        this.$set(this.printDeliveryOrderInfo, 'vendorPrincipal', this._get(res, 'data.data.vendorPrincipal', ''));
        this.$set(this.printDeliveryOrderInfo, 'customerPrincipal', this._get(res, 'data.data.customerPrincipal', ''));
        this.$set(this.printDeliveryOrderInfo, 'vendorPhone', this._get(res, 'data.data.vendorPhone', ''));
        this.$set(this.printDeliveryOrderInfo, 'customerPhone', this._get(res, 'data.data.customerPhone', ''));
      });
      this.$nextTick(() => {
        JsBarcode('#barcode', row.deliveryOrderNo, { height: 80, textAlign: 'left' });
      });
      getDetail(row.deliveryOrderId).then((res) => {
        this.printDeliveryDetailData = this._get(res, 'data.data', []);
      });
    },
    closePrintDeliveryOrderDialog() {
      this.printDeliveryOrderInfo = {};
      this.this.printDeliveryDetailData = [];
    },
    next() {
      if (this.deliveryDetailSelectionList.length === 0) {
        this.$message.warning('请选择一条送货明细!');
        return;
      }
      if (this.deliveryDetailSelectionList.length > 1) {
        this.$message.warning('只能选择一条送货明细!');
        return;
      }
      this.generateBarcodeDialog = true;
      this.generateBarcodeTitle = '生成条码';
      this.generateBarcodeOrderDetailData = this.deliveryDetailSelectionList;
      getVendorDetail(this.generateBarcodeOrderDetailData[0].vendorId).then((res) => {
        this.$set(this.packagingConfig, 'sequenceDefinitionId', this._get(res, 'data.data.sequenceDefinitionId', ''));
        this.$set(this.packagingConfig, 'sequenceDefinitionName', this._get(res, 'data.data.sequenceDefinitionName', ''));
      });
      this.querySequenceDetail();
      this.$nextTick(() => {
        $('#firstTheadChecked').on('change', (event) => {
          let $trs = $('.barCodeDetailTable').find('tbody tr');
          if (event.target.checked) {
            this._forEach($trs, (item) => {
              let $td1 = $(item).find('td')[1];
              let $td2 = $(item).find('td')[4];
              let $td3 = $(item).find('td')[7];
              $($td1).find('input').prop('checked', true);
              $($td2).find('input').prop('checked', false);
              $($td3).find('input').prop('checked', false);
              $('#secondTheadChecked').prop('checked', false);
              $('#thirdTheadChecked').prop('checked', false);
            });
          } else {
            this._forEach($trs, (item) => {
              let $td = $(item).find('td')[1];
              $($td).find('input').prop('checked', false);
            });
          }
        });
        $('#secondTheadChecked').on('change', (event) => {
          let $trs = $('.barCodeDetailTable').find('tbody tr');
          if (event.target.checked) {
            this._forEach($trs, (item) => {
              let $td1 = $(item).find('td')[1];
              let $td2 = $(item).find('td')[4];
              let $td3 = $(item).find('td')[7];
              $($td1).find('input').prop('checked', false);
              $($td2).find('input').prop('checked', true);
              $($td3).find('input').prop('checked', false);
              $('#firstTheadChecked').prop('checked', false);
              $('#thirdTheadChecked').prop('checked', false);
            });
          } else {
            this._forEach($trs, (item) => {
              let $td = $(item).find('td')[4];
              $($td).find('input').prop('checked', false);
            });
          }
        });
        $('#thirdTheadChecked').on('change', (event) => {
          let $trs = $('.barCodeDetailTable').find('tbody tr');
          if (event.target.checked) {
            this._forEach($trs, (item) => {
              let $td1 = $(item).find('td')[1];
              let $td2 = $(item).find('td')[4];
              let $td3 = $(item).find('td')[7];
              $($td1).find('input').prop('checked', false);
              $($td2).find('input').prop('checked', false);
              $($td3).find('input').prop('checked', true);
              $('#firstTheadChecked').prop('checked', false);
              $('#secondTheadChecked').prop('checked', false);
            });
          } else {
            this._forEach($trs, (item) => {
              let $td = $(item).find('td')[7];
              $($td).find('input').prop('checked', false);
            });
          }
        });
      });
    },
    querySequenceDetail() {
      getSequenceDetail(this.generateBarcodeOrderDetailData[0].sheetItemId).then((res) => {
        let obj = this._get(res, 'data.data', {});
        if (obj != null) {
          this.barCodePreviewInfo = obj;
          this.isGenerateBarCode = true;
          this.$set(this.packagingConfig, 'packMaxLevel', this.barCodePreviewInfo.packMaxLevel);
          this.$set(this.packagingConfig, 'sequenceDefinitionId', this.barCodePreviewInfo.sequenceDefinitionId);
          this.$set(this.packagingConfig, 'sequenceDefinitionName', this.barCodePreviewInfo.sequenceDefinitionName);
          this.$set(this.packagingConfig, 'packQuantitySpecFirst', this.barCodePreviewInfo.packQuantitySpecFirst);
          this.$set(this.packagingConfig, 'subPackQuantitySecond', this.barCodePreviewInfo.subPackQuantitySecond);
          this.$set(this.packagingConfig, 'subPackQuantityThird', this.barCodePreviewInfo.subPackQuantityThird);
        }
      });
    },
    changePackMaxLevel() {
      if (this.packagingConfig.packMaxLevel == 1) {
        this.packagingConfig.subPackQuantitySecond = '';
        this.packagingConfig.subPackQuantityThird = '';
      } else if (this.packagingConfig.packMaxLevel == 2) {
        this.packagingConfig.subPackQuantityThird = '';
      }
    },
    openDeliveryInfoDialog() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请至少选择一条数据！');
        return;
      }
      if (this.selectionList.length > 1) {
        let vendorName = this.selectionList[0].vendorName;
        for (let i = 0; i < this.selectionList.length; i++) {
          if (vendorName != this.selectionList[i].vendorName) {
            this.$message.warning('批量送货只能选择同个客户，请重新选择！');
            return;
          }
        }
      }
      this.deliveryDialog = true;
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.deliveryOrderId);
      });
      getDeliveryPreviewInfo(ids.join(',')).then((res) => {
        this.deliveryList = this._get(res, 'data.data', []);
      });
    },
    closeDeliveryDialog() {
      this.deliveryList = [];
    },
    previewBarCode() {
      if (!this.packagingConfig.packMaxLevel) {
        this.$message.error('包装层级不能为空！');
        return;
      }
      if (
        (this.packagingConfig.packMaxLevel == 1 || this.packagingConfig.packMaxLevel == 2 || this.packagingConfig.packMaxLevel == 3) &&
        !this.packagingConfig.packQuantitySpecFirst
      ) {
        this.$message.error('请输入1个小包包含的产品数量！');
        return;
      }
      if (
        (this.packagingConfig.packMaxLevel == 1 || this.packagingConfig.packMaxLevel == 2 || this.packagingConfig.packMaxLevel == 3) &&
        Number(this.packagingConfig.packQuantitySpecFirst) < 1
      ) {
        this.$message.error('包装层级1数量需为正整数！');
        return;
      }
      if ((this.packagingConfig.packMaxLevel == 2 || this.packagingConfig.packMaxLevel == 3) && !this.packagingConfig.subPackQuantitySecond) {
        this.$message.error('请输入1个大包包含的小包数量！');
        return;
      }
      if ((this.packagingConfig.packMaxLevel == 2 || this.packagingConfig.packMaxLevel == 3) && Number(this.packagingConfig.subPackQuantitySecond) < 1) {
        this.$message.error('包装层级2数量需为正整数！');
        return;
      }
      if (this.packagingConfig.packMaxLevel == 3 && !this.packagingConfig.subPackQuantityThird) {
        this.$message.error('请输入1个外箱包含的大包数量！');
        return;
      }
      if (this.packagingConfig.packMaxLevel == 3 && Number(this.packagingConfig.subPackQuantityThird) < 1) {
        this.$message.error('包装层级3数量需为正整数！');
        return;
      }
      if (this.isGenerateBarCode) {
        this.$message.warning('条码已生成，若需要修改条码，请先【报废条码】！');
        return;
      }
      let params = {
        deliverySheetItemId: this.generateBarcodeOrderDetailData[0].sheetItemId,
        sequenceDefinitionId: this.packagingConfig.sequenceDefinitionId,
        packMaxLevel: this.packagingConfig.packMaxLevel,
        packQuantitySpecFirst: this.packagingConfig.packQuantitySpecFirst,
        subPackQuantitySecond: this.packagingConfig.subPackQuantitySecond,
        subPackQuantityThird: this.packagingConfig.subPackQuantityThird
      };
      getPreviewBarCode(params).then((res) => {
        this.barCodePreviewInfo = this._get(res, 'data.data', {});
      });
    },
    generateBarcode() {
      if (this.barCodePreviewInfo.scmSequenceDetailDTOList && this.barCodePreviewInfo.scmSequenceDetailDTOList.length === 0) {
        this.$message.warning('请先【预览条码】！');
        return;
      }
      if (this.isGenerateBarCode) {
        this.$message.warning('条码已生成，若需要修改条码，请先【报废条码】！');
        return;
      }
      this.$confirm('是否确认生成条码？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {
          deliverySheetItemId: this.generateBarcodeOrderDetailData[0].sheetItemId,
          sequenceDefinitionId: this.packagingConfig.sequenceDefinitionId,
          packMaxLevel: this.packagingConfig.packMaxLevel,
          packQuantitySpecFirst: this.packagingConfig.packQuantitySpecFirst,
          subPackQuantitySecond: this.packagingConfig.subPackQuantitySecond,
          subPackQuantityThird: this.packagingConfig.subPackQuantityThird,
          firstSequenceNumber: this.barCodePreviewInfo.scmSequenceDetailDTOList[0].sequenceNumberFirst
        };
        getCreateBarCode(params).then((res) => {
          this.$message.success('条码已生成！');
          this.querySequenceDetail();
        });
      });
    },
    printBarCodeDetail() {
      if (this.barCodePreviewInfo.scmSequenceDetailDTOList && this.barCodePreviewInfo.scmSequenceDetailDTOList.length === 0) {
        this.$message.warning('请先【预览条码】！');
        return;
      }
      if (!this.isGenerateBarCode) {
        this.$message.warning('请先【生成条码】！');
        return;
      }
      let list1 = false;
      let list2 = false;
      let list3 = false;
      let $trs = $('.barCodeDetailTable').find('tbody tr');
      this._forEach($trs, (item) => {
        let $td1 = $(item).find('td')[1];
        let $td2 = $(item).find('td')[4];
        let $td3 = $(item).find('td')[7];
        let val1 = $($td1).find('input').prop('checked');
        let val2 = $($td2).find('input').prop('checked');
        let val3 = $($td3).find('input').prop('checked');
        if (val1) {
          list1 = true;
        }
        if (val2) {
          list2 = true;
        }
        if (val3) {
          list3 = true;
        }
      });
      if ((list1 && list2 && list3) || (list1 && list2) || (list1 && list3) || (list2 && list3)) {
        this.$message.warning('批量打印仅可勾选同一列！');
        return;
      }
      if (!list1 && !list2 && !list3) {
        this.$message.warning('请先勾选要打印的条码！');
        return;
      }
      this.printInfoDialog = true;
    },
    closePrintInfoDialog() {
      this.printInfo = {};
      this.$refs.printForm.resetFields();
    },
    scrapBarCode() {
      if (this.barCodePreviewInfo.scmSequenceDetailDTOList && this.barCodePreviewInfo.scmSequenceDetailDTOList.length === 0) {
        this.$message.warning('请先【预览条码】！');
        return;
      }
      if (!this.isGenerateBarCode) {
        this.$message.warning('请先【生成条码】！');
        return;
      }
      this.$confirm('报废后，所有的小包条码、大包条码、外箱条码都会报废，是否确认报废条码？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sequenceScrap(this.generateBarcodeOrderDetailData[0].sheetItemId).then((res) => {
          this.$message.success('条码已报废！');
          this.barCodePreviewInfo.scmSequenceDetailDTOList = [];
          this.isGenerateBarCode = false;
        });
      });
    },
    printSubmit() {
      if (!this.printInfo.labelTemplateName) {
        this.$message.warning('请选择标签模板！');
        return;
      }
      if (!this.printInfo.equipmentName) {
        this.$message.warning('请选择打印机！');
        return;
      }
      let $trs = $('.barCodeDetailTable').find('tbody tr');
      let $checkbox = [];
      let name = '';
      let sequenceDetailList = [];
      this._forEach($trs, (item) => {
        $checkbox = $(item).find("input[type='checkbox']:checked");
        if ($checkbox[0]) {
          name = $($checkbox[0]).attr('name');
          this._forEach(this.barCodePreviewInfo.scmSequenceDetailDTOList, (obj) => {
            if (obj.sequenceNumberFirst === name) {
              sequenceDetailList.push({
                sequenceGenerationId: obj.sequenceGenerationId,
                sequenceNumber: obj.sequenceNumberFirst
              });
            }
            if (obj.sequenceNumberSecond === name) {
              sequenceDetailList.push({
                sequenceGenerationId: obj.sequenceGenerationId,
                sequenceNumber: obj.sequenceNumberSecond
              });
            }
            if (obj.sequenceNumberThird === name) {
              sequenceDetailList.push({
                sequenceGenerationId: obj.sequenceGenerationId,
                sequenceNumber: obj.sequenceNumberThird
              });
            }
          });
        }
      });
      let nowArr = [];
      let obj = {};
      this._forEach(sequenceDetailList, (item) => {
        obj = this._find(nowArr, { sequenceNumber: item.sequenceNumber });
        if (!obj) {
          nowArr.push(item);
        }
      });
      let params = {
        sequenceDetailList: nowArr,
        equipmentId: this.printInfo.equipmentId,
        labelTemplateId: this.printInfo.labelTemplateId
      };
      sequencePirnt(params).then((res) => {
        this.$message.success(res.data.msg);
        this.printInfoDialog = false;
        $('.barCodeDetailTable').find('tr input[type="checkbox"]').prop('checked', false);
        queryDeliveryOrderDetail(this.selectDeliveryDetailPage.currentPage, this.selectDeliveryDetailPage.pageSize, Object.assign({ scmDeliveryOrderId: this.rowId })).then(
          (res) => {
            const data = res.data.data;
            this.selectDeliveryDetailPage.total = data.total;
            this.selectDeliveryDetailData = data.records;
            this._forEach(this.selectDeliveryDetailData, (item) => {
              item.isPrintedSeq = String(item.isPrintedSeq);
            });
            this.selectDeliveryDetailLoading = false;
          }
        );
        this.onLoad(this.page);
      });
    },
    submitDelivery() {
      this.$confirm('送货后，送货单将同步至客户MES系统，送货信息不可修改，是否确认送货？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let data = [];
        this._forEach(this.selectionList, (item) => {
          data.push(item.deliveryOrderId);
        });
        this.deliveryLoading = true;
        delivering(data).then(
          (res) => {
            this.$message.success('同步成功');
            this.deliveryLoading = false;
            this.deliveryDialog = false;
            this.onLoad(this.page);
          },
          (error) => {
            this.deliveryLoading = false;
          }
        );
      });
    },
    /**
     * 获取打印机
     */
    openPrintersModal() {
      this.modalName = 'printersModal';
      this.$refs.commonModalTemplate.showModal();
      this.tableOption = {
        modalTitle: '选择打印机（双击进行选择）',
        url: '/api/mes-product/scm-printer/page-query',
        isPage: true,
        rowSelectionType: 'single',
        columnDefs: [
          {
            label: '打印机编码',
            prop: 'code',
            search: true,
            searchLabelWidth: 110
          },
          {
            label: '打印机名称',
            prop: 'name',
            search: true,
            searchLabelWidth: 110
          }
        ]
      };
    },
    /**
     * 打开标签模板模态框
     */
    openLabelTemplateModal() {
      this.modalName = 'labelTemplateModal';
      this.$refs.commonModalTemplate.showModal();
      this.tableOption = {
        modalTitle: '选择标签模版（双击进行选择）',
        url: `/api/mes-admin/labeltemplate/list-by-partnerid?partnerId=${this.generateBarcodeOrderDetailData[0].vendorId}`,
        isPage: false,
        rowSelectionType: 'single',
        columnDefs: [
          {
            label: '标签名称',
            prop: 'fileName',
            search: true
          },
          {
            label: '标签代码',
            prop: 'code',
            search: true
          },
          {
            label: '版本名称',
            prop: 'revision'
          },
          {
            label: '缩略图',
            prop: 'thumbnailUrl'
          },
          {
            label: '打印标签',
            prop: 'fileUrl'
          }
        ]
      };
    },
    handleAfterPrint() {
      deliveryOrderPrint(this.rowId).then((res) => {
        this.$message.success(res.data.msg);
        this.printDeliveryOrderDialog = false;
        this.onLoad(this.page);
      });
    }
  }
};
</script>

<style scoped lang="scss">
.custom-title {
  font-size: 14px;
  font-weight: bold;
  margin: 10px 0;
  i {
    margin-right: 10px;
  }
}
::v-deep .avue-form__group {
  .el-date-editor {
    justify-content: space-between;
    .el-input__icon {
      display: none;
    }
    input {
      width: 43%;
    }
  }
}
::v-deep .el-dialog__body {
  .avue-crud__menu {
    display: none;
  }
}
::v-deep .el-table__header {
  thead {
    th {
      background-color: #fafafa;
      padding: 8px 0;
      color: rgba(0, 0, 0, 0.85);
      font-size: 12px;
    }
  }
}
#printContent {
  width: 100%;
  table {
    page-break-inside: auto;
    tr {
      page-break-inside: avoid;
      page-break-after: auto;
      height: 30px;
    }
    th {
      white-space: nowrap;
      height: 30px;
      font-size: 12px;
      background: #f2f2f2;
    }
    td {
      padding: 0 5px;
    }
    thead {
      display: table-header-group;
    }
    tfoot {
      display: table-footer-group;
    }
  }
}
.barCodeDetailTable {
  border-collapse: collapse;
  thead,
  tbody tr {
    display: table;
    width: 100%;
    table-layout: fixed; /* 这确保了列宽固定 */
  }
  th,
  td {
    padding: 8px;
    text-align: left;
  }
  thead {
    display: table;
    width: calc(100% - 10px);
    table-layout: fixed;
    th {
      height: 30px;
      font-size: 12px;
      background: #f2f2f2;
    }
  }
  tbody {
    height: 300px;
    display: block;
    overflow-y: auto; /* 添加垂直滚动条 */
    overflow-x: hidden;
  }
}
</style>
<style @media="print">
@page {
  size: auto;
  margin: 3mm;
}

html {
  background-color: #ffffff;
  height: auto;
  margin: 0px;
}
</style>
